package br.com.Login.controller.filtros;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.Login.controller.UserMB;

public class FiltroAcessoLogin implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
				
		System.out.println("Executando Filtro de acesso ao login...");
		UserMB userMB = (UserMB) ((HttpServletRequest) request).getSession().getAttribute("userMB");
		
		if(userMB == null || userMB.getUser() == null || userMB.getLoggedin() == false){
			chain.doFilter(request, response);
			
		}else if(userMB.getLoggedin() == true){
			String contextPath = request.getServletContext().getContextPath();
			HttpServletResponse r = (HttpServletResponse) response;
			r.sendRedirect(contextPath+"/pages/index.xhtml");
		}
		System.out.println("Filtro de acesso ao login executado.");

	}
	
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub	
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	
}
